<?php

namespace app\admin\model;

use think\Model;

/**
 * 用户模型
 * @package app\admin\model
 */
class Users extends Model
{
    /**
     * 用户指定字段值
     * @param $where
     * @param string $field
     * @return mixed
     */
    public static function findUsersValue($where, $field)
    {
        return self::where($where)->value($field);
    }

    /**
     * 本月用户数据
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getUsersMonth($startTime, $endTime)
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->group('create_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 本周用户数据
     * @return array
     */
    public static function getUsersWeek()
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereWeek('create_time')
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 本周新增用户
     * @return int
     */
    public static function countUsersWeekCreate()
    {
        return self::whereWeek('create_time')->count();
    }

    /**
     * 用户总数
     * @param array $where
     * @return int
     */
    public static function countUsers($where = [])
    {
        return self::whereNotIn('associate', [-1, -2])->where($where)->count();
    }

    /**
     * 昨日新增用户
     * @param array $where
     * @return int
     */
    public static function yesTodayUsers($where = [])
    {
        return self::whereDay('create_time', 'yesterday')->where($where)->count();
    }

    /**
     * 实时新增用户
     * @param array $where
     * @return int
     */
    public static function todayUsers($where = [])
    {
        return self::whereDay('create_time')->where($where)->count();
    }

    /**
     * 用户列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getUsersList($where, $page, $pagesize)
    {
        $data = self::alias('u')
            ->join('users_cate c', 'c.uid = u.id', 'left')
            ->join('identity i', 'i.id = c.identity_id', 'left')
            ->join('users_member_cp m', 'm.uid = u.id', 'left')
            ->where($where)
//            ->fetchSql()
            ->field('u.id,u.headimg,u.nickname,u.auth_city,u.role,u.associate,u.status,u.create_time,u.state,i.name as iname,m.end_time')
            ->order('u.create_time', 'desc')
            ->page($page, $pagesize)
            ->select();
//        var_dump($data);exit;

        return $data ? $data->toArray() : [];
    }

    /**
     * 用户列表统计
     * @param $where
     * @return mixed
     */
    public static function countUsersList($where)
    {
        return self::alias('u')
            ->join('users_cate c', 'c.uid = u.id', 'left')
            ->join('identity i', 'i.id = c.identity_id', 'left')
            ->join('users_member_cp m', 'm.uid = u.id', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 用户数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findUsers($where)
    {
        $info = self::where($where)->find();

        return $info ? $info->toArray() : [];
    }

    /**
     * 更新用户信息
     * @param $data
     * @param $where
     * @return Users
     */
    public static function updateUsers($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 用户地区分布
     * @return mixed
     */
    public static function getUsersArea()
    {
        $data = self::fieldRaw("COUNT(*) nums,auth_city as name")
            ->group('auth_city')
            ->order('nums', 'desc')
            ->limit(6)
            ->select();

        return $data ? $data->toArray() : [];
    }

}